﻿//-----------------------------------------------------------------------
// <copyright file="AuthorizationServiceBase.cs" company="Alan Rodrigues">
//     Copyright (c) Alan Rodrigues. All rights reserved.
// </copyright>
// <developed>Alan Rodrigues (operninha@gmail.com)</developed>
//-----------------------------------------------------------------------

namespace Authorization
{
    using System;
    using Properties;

    public partial class AuthorizationServiceBase
    {
        #region Applications

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public ApplicationCollection GetAllApplications()
        {
            return AuthorizationStoreManager.GetApplications();
        }

        public string[] GetAllApplicationNames()
        {
            return AuthorizationStoreManager.GetApplicationNames();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="applicationName"></param>
        /// <returns></returns>
        public Application GetApplication(string applicationName)
        {
            return AuthorizationStoreManager.GetApplication(applicationName);
        }

        public bool ApplicationExists(string applicationName)
        {
            SecUtility.CheckParameter(ref applicationName, true, true, false, 64, "applicationName");
            return AuthorizationStoreManager.ApplicationExists(applicationName);
        }

        public void DeleteApplication(string applicationName)
        {
            SecUtility.CheckParameter(ref applicationName, true, true, false, 64, "applicationName");
            var store = AuthorizationStoreManager.DeleteApplication(applicationName);
            store.Save();
        }

        public Application CreateApplication(string storeName, string applicationName, string version, string Description, string applicationData)
        {
            SecUtility.CheckParameter(ref applicationName, true, true, false, 64, "applicationName");

            var application = AuthorizationStoreManager.CreateApplication(storeName, applicationName, version, Description, applicationData);

            application.Save();
            return application;
        }

        public Application UpdateApplication(string applicationName, string newApplicationName, string newVersion, string newDescription, string newApplicationData)
        {
            SecUtility.CheckParameter(ref applicationName, true, true, false, 64, "applicationName");

            var application = AuthorizationStoreManager.UpdateApplication(applicationName, newApplicationName, newVersion, newDescription, newApplicationData);
            application.Save();
            return application;
        }

        public string ExportApplication(string applicationName, bool isToMerge, bool isCopyWithAdministrators, bool isCopyWithUsers)
        {
            SecUtility.CheckParameter(ref applicationName, true, true, false, 64, "applicationName");

            var application = AuthorizationStoreManager.GetApplication(applicationName);

            return application.AuthorizationStore.ExportApplication(application, isToMerge, isCopyWithAdministrators, isCopyWithUsers);
        }

        public string ImportApplication(string applicationName, string xmlData, string store, bool isToMerge)
        {
            var application = AuthorizationStoreManager.GetApplication(applicationName);
            AuthorizationStore destStore = null;

            if (application == null)
            {                
                if (String.IsNullOrEmpty(store))
                    store = AuthorizationStoreManager.GetDefaultStoreName();

                destStore = AuthorizationStoreManager.GetStoreByName(store);
            }
            else
            {
                destStore = application.AuthorizationStore;
            }

            return AuthorizationStoreManager.ImportApplication(xmlData, applicationName, destStore, isToMerge);
        }

        #endregion
    }
}
